$.extend($.fn.layout.methods, {   
    /**  
     * 面板是否存在和可见  
     * @param {Object} jq  
     * @param {Object} params  
     */  
    isVisible: function(jq, params) {   
        var panels = $.data(jq[0], 'layout').panels;   
        var pp = panels[params];   
        if(!pp) {   
            return false;   
        }
        if(pp.length) {
            return pp.panel('panel').is(':visible');   
        } else {
            return false;   
        }
    },
    /**  
     * 隐藏除某个region，center除外。  
     * @param {Object} jq  
     * @param {Object} params  
     */  
    hidden: function(jq, params) {
        return jq.each(function() {
            var opts = $.data(this, 'layout').options;   
            var panels = $.data(this, 'layout').panels;   
            if(!opts.regionState){
                opts.regionState = {};   
            }   
            var region = params;   
            function hide(dom,region,doResize){   
                var first = region.substring(0,1);   
                var others = region.substring(1);   
                var expand = 'expand' + first.toUpperCase() + others;   
                if(panels[expand]) {   
                    if($(dom).layout('isVisible', expand)) {   
                        opts.regionState[region] = 1;   
                        panels[expand].panel('close');   
                    } else if($(dom).layout('isVisible', region)) {   
                        opts.regionState[region] = 0;   
                        panels[region].panel('close');   
                    }   
                } else {   
                    panels[region].panel('close');   
                }   
                if(doResize){   
                    $(dom).layout('resize');   
                }   
            };   
            if(region.toLowerCase() == 'all'){   
                hide(this,'east',false);   
                hide(this,'north',false);   
                hide(this,'west',false);   
                hide(this,'south',true);   
            }else{   
                hide(this,region,true);   
            }   
        });   
    },   
    /**  
     * 显示某个region，center除外。  
     * @param {Object} jq  
     * @param {Object} params  
     */  
    show: function(jq, params) {   
        return jq.each(function() {   
            var opts = $.data(this, 'layout').options;   
            var panels = $.data(this, 'layout').panels;   
            var region = params;   
  
            function show(dom,region,doResize){   
                var first = region.substring(0,1);   
                var others = region.substring(1);   
                var expand = 'expand' + first.toUpperCase() + others;   
                if(panels[expand]) {   
                    if(!$(dom).layout('isVisible', expand)) {   
                        if(!$(dom).layout('isVisible', region)) {   
                            if(opts.regionState[region] == 1) {   
                                panels[expand].panel('open');   
                            } else {   
                                panels[region].panel('open');   
                            }   
                        }   
                    }   
                } else {   
                    panels[region].panel('open');   
                }   
                if(doResize){   
                    $(dom).layout('resize');   
                }   
            };   
            if(region.toLowerCase() == 'all'){   
                show(this,'east',false);   
                show(this,'north',false);   
                show(this,'west',false);   
                show(this,'south',true);   
            }else{   
                show(this,region,true);   
            }   
        });   
    }   
});
$.extend($.fn.panel.methods, {
	iframe : function(jq, href) {
		return jq.each(function() {
			$(this).html('<iframe src="' + href + '" frameborder=0 height=100% width=100% scrolling=auto></iframe>');
		});
	}
});
$.extend($.fn.datagrid.methods, {
	loader : function(_76c, _76d, _76e) {
		var opts = $(this).datagrid("options");
		if (!opts.url) {
			return false;
		}
		alert(opts.url);
		$.ajax({
			type : opts.method,
			url : opts.url,
			data : _76c,
			dataType : "json",
			success : function(data) {
				_76d(data);
			},
			error : function() {
				_76e.apply(this, arguments);
			}
		});
	},
	/**
	 * 携带条件重新加载
	 */
    reloadWithCriteria : function(jq, param) {
        var grid = $(jq);
        var toolbar = grid.datagrid('options').toolbar;
    	if(toolbar) {
    		var form = new ListForm(toolbar);
    		var map = new Map(grid.datagrid("options").queryParams).append(new Map(form.getData()));
    		map.clearNullAndEmptyValue();
    		grid.datagrid("load", map.toJson());
    		grid.datagrid("clearSelections");
    	}
    },
    /**
	 * 对选中项进行指定操作
	 */
	executeSelected : function(jq, param) {
		var action = param.action;
		if(!action) {
			action = "执行";
		}
	    var grid = $(jq);
	    var rows = grid.datagrid("getSelections");
	    if(rows.length == 0) {
	    	$.messager.alert('提示', '请先选择要操作的内容', 'info');
	    	return;
	    }
		var ids = new List();
		new List(rows).each(function(row){
			ids.add(row.id);
		});
		var json = {"ids": ids.toString()};
		$.messager.confirm('提示', '您确定要对选中的内容执行'+action+'操作？', function(result){
			if(result) {
				ajax({
					url:param.url, 
					data:json, 
					success:function() {
						if(param.success) {
							param.success(ids);
						}
						grid.datagrid("reloadWithCriteria");
					}
				});
			}
		});
	},
	/**
	 * 删除选中项
	 */
	deleteSelected : function(jq, param) {
		param.action = "删除";
	    var grid = $(jq).datagrid("executeSelected", param);
	},
	/**
	 * 取消所有选中行，只选中当前指定行
	 */
    onlySelectThisRow : function(jq, param) {
        var grid = $(jq);
        grid.datagrid('clearSelections');
        grid.datagrid('selectRow',param.rowIndex);
    }
});
function onlySelectDatagridRow(rowIndex, rowData) {
	$(this).datagrid('onlySelectThisRow', {rowIndex: rowIndex});
}
$.extend($.fn.tree.methods, {
	/**
	 * 对选中项进行指定操作
	 */
	executeSelected : function(jq, param) {
		var action = param.action;
		if(!action) {
			action = "执行";
		}
	    var tree = $(jq);
	    var selected = tree.tree("getSelected");
	    var json = {ids: selected.id};
	    $.messager.confirm('提示', '您确定要对选中的内容执行'+action+'操作？', function(result){
			if(result) {
				var parent = tree.tree('getParent',selected.target);
				if(parent) {
					tree.tree('select',parent.target);
				}
				ajax({
					url:param.url, 
					data:json, 
					success:function() {
						if(param.success) {
							param.success(parent);
						}
						if(parent) {
							tree.tree('reload',parent.target);
						} else {
							tree.tree('reload');
						}
					}
				});
			}
		});
	},
	/**
	 * 删除选中项
	 */
    deleteSelected : function(jq, param) {
    	param.action = "删除";
        $(jq).tree('executeSelected', param);
    },
    /**
     * 获取selected节点的子节点
     */
    getNextLevelNodes : function(jq, param) {
    	var nodes = [];
    	$(param).next().children().children("div.tree-node").each(function(){
            nodes.push($(jq[0]).tree('getNode',this));
        });
        return nodes;
    }
});
$.extend($.fn.treegrid.methods, {
	/**
	 * 对选中项进行指定操作
	 */
    executeSelected : function(jq, param) {
    	var action = param.action;
		if(!action) {
			action = "执行";
		}
        var treegrid = $(jq);
        var selections = treegrid.treegrid("getSelections");
        if(selections.length == 0) {
	    	$.messager.alert('提示', '请先选择要操作的内容', 'info');
	    	return;
	    }
    	var ids = new List();
    	new List(selections).each(function(selection){
    		ids.add(selection.id);
    	});
    	var json = {ids: ids.toString()};
    	$.messager.confirm('提示', '您确定要对选中的内容执行'+action+'操作？', function(result){
    		if(result) {
    			var parent = treegrid.treegrid('getParent',selections[0].id);
    			if(parent) {
    				treegrid.treegrid("clearSelections");
    				treegrid.treegrid('select',parent.id);
    			}
    			ajax({
    				url:param.url, 
    				data:json, 
    				success:function(data) {
    					if(param.success) {
    						param.success(data);
    					}
    					if(parent) {
    						treegrid.treegrid('reload',parent.id);
    					} else {
    						treegrid.treegrid('reloadWithCriteria');
    					}
    				}
    			});
    		}
    	});
    },
    /**
	 * 删除选中项
	 */
    deleteSelected : function(jq, param) {
    	param.action = "删除";
        $(jq).treegrid("executeSeleted", param);
    },
    /**
	 * 携带条件重新加载
	 */
    reloadWithCriteria : function(jq, param) {
        var treegrid = $(jq);
        var toolbar = treegrid.treegrid('options').toolbar;
    	if(toolbar) {
    		var form = new ListForm(toolbar);
    		var map = new Map(form.getData());
    		map.clearNullAndEmptyValue();
    		treegrid.treegrid("load", map.toJson());
    		treegrid.treegrid("clearSelections");
    	}
    },
    /**
	 * 重新加载父节点
	 */
    reloadParent : function(jq, param) {
        var treegrid = $(jq);
        var node = treegrid.treegrid('getSelected');
        treegrid.treegrid('unselect',node.id)
		node = treegrid.treegrid('getParent',node.id);
		if(node) {
			treegrid.treegrid('select',node.id)
			treegrid.treegrid('reload',node.id);
		} else {
			treegrid.treegrid('reloadWithCriteria');
		}
    },
    /**
	 * 重新加载当前选中节点
	 */
    reloadSelected : function(jq, param) {
        var treegrid = $(jq);
        var node = treegrid.treegrid('getSelected');
		if(node) {
			treegrid.treegrid('reload',node.id);
		} else {
			treegrid.treegrid('reloadWithCriteria');
		}
    },
    /**
	 * 取消所有选中行，只选中当前指定行
	 */
    onlySelectThisRow : function(jq, param) {
        var treegrid = $(jq);
        treegrid.treegrid('clearSelections');
        treegrid.treegrid('select',param.id);
    }
});
function onlySelectTreegridRow(row) {
	$(this).treegrid('onlySelectThisRow', row);
}
$.extend($.fn.textbox.methods, {
	focus: function(jq) {
		$(jq).textbox('textbox').focus();
	}
});
eui.alert = function(msg, fn) {
	$.messager.alert("提示", msg, "info", fn);
};
eui.warn = function(msg, fn) {
	$.messager.alert("警告", msg, "warn", fn);
};
eui.error = function(msg, fn) {
	$.messager.alert("错误", msg, "error", fn);
};
eui.msg = function(msg, position, type) {
	if(!position) position = "center";
	if(!type) type = "slide";
	var style;
	if(position == "topCenter") {
		style = {
			right:'',
			top:document.body.scrollTop+document.documentElement.scrollTop,
			bottom:''
		};
	} else if(position == "topLeft") {
		style = {
			right:'',
			left:0,
			top:document.body.scrollTop+document.documentElement.scrollTop,
			bottom:''
		};
	} else if(position == "topRight") {
		style = {
			left:'',
			right:0,
			top:document.body.scrollTop+document.documentElement.scrollTop,
			bottom:''
		};
	} else if(position == "center") {
		style = {
			right:'',
			bottom:''
		};
	} else if(position == "bottomLeft") {
		style = {
			left:0,
			right:'',
			top:'',
			bottom:-document.body.scrollTop-document.documentElement.scrollTop
		};
	} else if(position == "bottomCenter") {
		style = {
			right:'',
			top:'',
			bottom:-document.body.scrollTop-document.documentElement.scrollTop
		};
	} else if(position == "bottomRight") {
		style = {};
	}
	$.messager.show({
		title:'提示',
		msg:msg,
		showSpeed:100,
		timeout:500,
		shadow:true,
		closable:false,
		showType:type,
		style:style
	});
};